package net.qunzhi.modules.cms.service.impl;

import net.qunzhi.common.utils.Constant;
import net.qunzhi.modules.cms.entity.TbEquipmentEntity;
import net.qunzhi.modules.cms.query.StateDateQuery;
import net.qunzhi.modules.historyspider.HistorySpiderStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import net.qunzhi.common.utils.BaseQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.Query;

import net.qunzhi.modules.cms.dao.TbPressureStateDao;
import net.qunzhi.modules.cms.entity.TbPressureStateEntity;
import net.qunzhi.modules.cms.service.TbPressureStateService;


@Service("tbPressureStateService")
public class TbPressureStateServiceImpl extends ServiceImpl<TbPressureStateDao, TbPressureStateEntity> implements TbPressureStateService {

    protected static Logger logger = LoggerFactory.getLogger(TbPressureStateServiceImpl.class);

    @Override
    public PageUtils queryPage(StateDateQuery query) {
        IPage<TbPressureStateEntity> page = this.page(
                new Query<TbPressureStateEntity>().getPage(query),
                new QueryWrapper<TbPressureStateEntity>()
                        .eq("equipment_id", query.getEquipmentId())
                        .le("time", query.getEndDateTime())
                        .ge("time", query.getStartDateTime())
                        .orderByDesc("time")
        );

        return new PageUtils(page);
    }

    @Override
    public void addPressureData(TbEquipmentEntity entity) {
        try {
            //获取最大时间
            String maxTime = this.baseMapper.getMaxTimeByEquipmentId(entity.getId());

            HistorySpiderStarter.work(entity, maxTime, Constant.SpiderType.PRESSURE.getValue(), entity.getTag());
        }catch (Exception e){
            logger.error(e.getMessage());
        }finally {
        }
    }




}